
package com.net.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.net.annotaion.ApiLog;
import com.net.controller.model.ResponseModel;
import com.net.controller.vo.EditWebsiteParam;
import com.net.controller.vo.UserFlowResponse;
import com.net.controller.vo.WebsiteResponseModel;
import com.net.model.User;
import com.net.model.UserFlow;
import com.net.model.UserFlowStatData;
import com.net.model.Website;
import com.net.service.vo.ServiceCode;
import com.net.service.website.WebsiteService;
import com.net.util.ResponseEntityHelper;
import com.net.util.SpringUtils;

@Controller
@RequestMapping(value = "/net/detect")
public class FlowStatApi extends SuperController {
	WebsiteService websiteService;

	@Resource(name = "websiteService")
	public void setWebsiteService(WebsiteService websiteService) {
		this.websiteService = websiteService;
	}

	@ApiLog
	@RequestMapping(value = "/userflow/tolistpage")
	public ModelAndView toListPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("flow/user_flow_stat");
		return mv;
	}

	@ApiLog
	@RequestMapping(value = "/userflow/list", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> listPageUserFlow(HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		UserFlowStatData userFlowStatData = SpringUtils.getBean("user_flow_data");
		System.out.println(userFlowStatData);
		// 模拟数据
		/*Double[] flowPerMinute = new Double[30];
		for (int i = 0; i < 30; i++) {
			flowPerMinute[i] = (double) i;
		}

		for (int i = 0; i < 30; i++) {
			System.out.println(flowPerMinute[i]);
		}
		Map<String, Double> flowPerType = new HashMap<String, Double>();
		flowPerType.put("视频", 11111.1);
		flowPerType.put("游戏", 222222.2);
		flowPerType.put("学习", 33333.3);
		flowPerType.put("聊天", 444444.4);

		UserFlow userFlow = new UserFlow();
		userFlow.setFlowPerMinute(flowPerMinute);
		userFlow.setFlowPerType(flowPerType);
		userFlowStatData.getData().put("192.168.3.2", userFlow);
		userFlowStatData.getData().put("192.168.3.3", userFlow);*/

		List<UserFlowResponse> userFlowList = new ArrayList<UserFlowResponse>();
		Map<String, UserFlow> data = userFlowStatData.getData();
		Iterator iter = data.entrySet().iterator();
		while (iter.hasNext()) {
			UserFlowResponse userFlowResponse = new UserFlowResponse();
			
			Map.Entry entry = (Map.Entry) iter.next();
			String ip = (String) entry.getKey();
			UserFlow tempUserFlow = (UserFlow) entry.getValue();
			Map<String, Double> tempFlowPerType = tempUserFlow.getFlowPerType();
			// 取出数据
			userFlowResponse.setIp(ip);
			userFlowResponse.setFlowPerMinute(tempUserFlow.getFlowPerMinute());
			
			Iterator iter2 = tempFlowPerType.entrySet().iterator();
			String[][] flowPerTypes = new String[tempFlowPerType.size()][2];
			int i = 0;
			while (iter2.hasNext()) {
				Map.Entry entry2 = (Map.Entry) iter2.next();
				String type = (String) entry2.getKey();
				Double val = (Double) entry2.getValue();
				flowPerTypes[i][0] = type;
				flowPerTypes[i][1] = String.valueOf(val);
				i++;
			}
			userFlowResponse.setFlowPerType(flowPerTypes);
			userFlowList.add(userFlowResponse);
		}

		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "获取成功", userFlowList);
	}

}
